﻿using System.Net;
using System.Web.Mvc;
using System.Linq;
using TestApplication.Domain.Models;

namespace TestApplication.WebUI.Areas.Manage.Controllers
{
    public class StoresController : Controller
    {
        private readonly UnitOfWork _unitOfWork;

        public StoresController(UnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public ActionResult Index()
        {
            return View(_unitOfWork.StoreRepository.AsQueryable.ToList());
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Title,Address,WorkingHours")] Store store)
        {
            if (!ModelState.IsValid)
            {
                return View(store);
            }

            _unitOfWork.StoreRepository.Add(store);
            _unitOfWork.SaveChanges();
            return RedirectToAction("Details", new { id = store.Id });
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var store = _unitOfWork.StoreRepository.Find(id);
            if (store == null)
            {
                return HttpNotFound();
            }

            return View(store);
        }

        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var store = _unitOfWork.StoreRepository.Find(id);
            if (store == null)
            {
                return HttpNotFound();
            }

            return View(store);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Title,Address,WorkingHours")] Store store)
        {
            if (!ModelState.IsValid)
            {
                return View(store);
            }

            _unitOfWork.StoreRepository.Add(store);
            _unitOfWork.SaveChanges();
            return RedirectToAction("Details", new { id = store.Id });
        }

        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var product = _unitOfWork.StoreRepository.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }

            return View(product);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            _unitOfWork.StoreRepository.Remove(id);
            _unitOfWork.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}